package com.luo.chengrui.module.sa.controller.admin.datapermission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionItemSubDO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionItemSubConvert;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionItemSubService;

@Tag(name = "管理后台 - 数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。")
@RestController
@RequestMapping("/sa/data-permission-item-sub")
@Validated
public class DataPermissionItemSubController {

    @Resource
    private DataPermissionItemSubService dataPermissionItemSubService;

    @PostMapping("/create")
    @Operation(summary = "创建数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:create')")
    public CommonResult<String> createDataPermissionItemSub(@Valid @RequestBody DataPermissionItemSubCreateReqVO createReqVO) {
        return success(dataPermissionItemSubService.createDataPermissionItemSub(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:update')")
    public CommonResult<Boolean> updateDataPermissionItemSub(@Valid @RequestBody DataPermissionItemSubUpdateReqVO updateReqVO) {
        dataPermissionItemSubService.updateDataPermissionItemSub(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:delete')")
    public CommonResult<Boolean> deleteDataPermissionItemSub(@RequestParam("id") String id) {
        dataPermissionItemSubService.deleteDataPermissionItemSub(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:query')")
    public CommonResult<DataPermissionItemSubRespVO> getDataPermissionItemSub(@RequestParam("id") String id) {
        DataPermissionItemSubDO dataPermissionItemSub = dataPermissionItemSubService.getDataPermissionItemSub(id);
        return success(DataPermissionItemSubConvert.INSTANCE.convert(dataPermissionItemSub));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:query')")
    public CommonResult<List<DataPermissionItemSubRespVO>> getDataPermissionItemSubList(@RequestParam("ids") Collection<String> ids) {
        List<DataPermissionItemSubDO> list = dataPermissionItemSubService.getDataPermissionItemSubList(ids);
        return success(DataPermissionItemSubConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。分页")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:query')")
    public CommonResult<PageResult<DataPermissionItemSubRespVO>> getDataPermissionItemSubPage(@Valid DataPermissionItemSubPageReqVO pageVO) {
        PageResult<DataPermissionItemSubDO> pageResult = dataPermissionItemSubService.getDataPermissionItemSubPage(pageVO);
        return success(DataPermissionItemSubConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。 Excel")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-item-sub:export')")
    @OperateLog(type = EXPORT)
    public void exportDataPermissionItemSubExcel(@Valid DataPermissionItemSubExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataPermissionItemSubDO> list = dataPermissionItemSubService.getDataPermissionItemSubList(exportReqVO);
        // 导出 Excel
        List<DataPermissionItemSubExcelVO> datas = DataPermissionItemSubConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据权限项明细，代码中用来配置SaDataPermission注解中变量permissionCode的取值。.xls", "数据", DataPermissionItemSubExcelVO.class, datas);
    }

}
